package com.example.weather_forecast;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder> {

    private List<WeatherData> weatherDataList;

    public WeatherAdapter(List<WeatherData> weatherDataList) {
        this.weatherDataList = weatherDataList != null ? weatherDataList : new ArrayList<>();
    }

    public void updateData(List<WeatherData> newData) {
        this.weatherDataList = newData != null ? newData : new ArrayList<>();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public WeatherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_weather_forecast, parent, false);
        return new WeatherViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHolder holder, int position) {
        WeatherData weatherData = weatherDataList.get(position);

        holder.locationTextView.setText(weatherData.getLocation());
        // 将 Date 类型转换为合适的字符串展示格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        holder.dateTextView.setText(dateFormat.format(weatherData.getDate()));

        // 根据 weatherIcon 内容设置实际的天气图标
        int iconResId = getWeatherIconResource(weatherData.getWeatherIcon());
        holder.weatherIconImageView.setImageResource(iconResId);

        holder.currentTempTextView.setText(weatherData.getCurrentTemp());
        holder.tempRangeTextView.setText(weatherData.getTempRange());
        holder.weatherConditionTextView.setText(weatherData.getWeatherCondition());
    }

    @Override
    public int getItemCount() {
        return weatherDataList.size();
    }

    public static class WeatherViewHolder extends RecyclerView.ViewHolder {
        TextView locationTextView;
        TextView dateTextView;
        ImageView weatherIconImageView;
        TextView currentTempTextView;
        TextView tempRangeTextView;
        TextView weatherConditionTextView;

        public WeatherViewHolder(@NonNull View itemView) {
            super(itemView);
            locationTextView = itemView.findViewById(R.id.locationTextView);
            dateTextView = itemView.findViewById(R.id.dateTextView);
            weatherIconImageView = itemView.findViewById(R.id.weatherIconImageView);
            currentTempTextView = itemView.findViewById(R.id.currentTempTextView);
            tempRangeTextView = itemView.findViewById(R.id.tempRangeTextView);
            weatherConditionTextView = itemView.findViewById(R.id.weatherConditionTextView);
        }
    }

    private int getWeatherIconResource(String weatherIcon) {
        // 根据 weatherIcon 字符串返回对应的资源 ID
        switch (weatherIcon) {
            case "ic_sunny":
                return R.drawable.ic_sunny;
            case "ic_cloudy":
                return R.drawable.ic_cloudy;
            // 可以添加更多的天气图标映射
            default:
                return R.drawable.default_weather_icon;
        }
    }
}